package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.ConfigDao;
import com.wanlian.dao.DeviceDao;
import com.wanlian.entity.Config;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.ConfigServices;
import com.wanlian.vo.VersionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ConfigServicesImpl implements ConfigServices {


    @Autowired
    public ConfigDao configDao;

    @Autowired
    public DeviceDao deviceDao;

    @Override
    public ResultModel doAdd(Config config) {
        ResultModel resultModel = new ResultModel();
        Config dbconfig=configDao.queryByKey(config.getCkey());
        if(dbconfig!=null){
            resultModel.setBresult(false);
            resultModel.setMessage("信息已存在");
            return resultModel;
        }
        config.setEnable(true);
        configDao.add(config);
        resultModel.setBresult(true);
        resultModel.setMessage("添加成功");
        return resultModel;
    }

    @Override
    public ResultModel doAdd(VersionVo versionVo) {
        ResultModel resultModel = new ResultModel();
        Config dbConfig=configDao.queryByKey(versionVo.getVersionType());
        if(dbConfig!=null){
            resultModel.setBresult(false);
            resultModel.setMessage("版本信息已存在");
            return resultModel;
        }
        Map<String, String> map = new HashMap<String, String>();
        Config config = new Config();
        map.put("versionCode", versionVo.getVersionCode());
        map.put("versionName", versionVo.getVersionName());
        map.put("updateLog", versionVo.getUpdateLog());
        //map.put("isForceUpdata", versionVo.isForceUpdata() +"");
        map.put("downUrl", versionVo.getDownUrl());
        String value = JsonStringUtil.writeValueAsString(map);
        config.setCkey(versionVo.getVersionType());
        config.setCvalue(value);
        config.setEnable(true);
        configDao.add(config);
        resultModel.setBresult(true);
        resultModel.setMessage("添加成功");
        return resultModel;
    }

    @Override
    public ResultModel doeditDisable(Config config) {
        ResultModel resultModel = new ResultModel();
        Config dbconfig = configDao.queryById(config.getId());
        dbconfig.setEnable(false);
        configDao.update(dbconfig);
        resultModel.setBresult(true);
        resultModel.setMessage("禁用成功");
        return resultModel;
    }

    @Override
    public ResultModel doeditEnable(Config config) {
        ResultModel resultModel = new ResultModel();
        Config dbconfig = configDao.queryById(config.getId());
        dbconfig.setEnable(true);
        configDao.update(dbconfig);
        resultModel.setBresult(true);
        resultModel.setMessage("启用成功");
        return resultModel;
    }

    @Override
    public ResultModel queryConfigList(PageCondition pageCondition) {
        return configDao.queryConfigList(pageCondition);
    }


    @Override
    public ResultModel doUpd(Config config) {
        ResultModel resultModel = new ResultModel();
        Config dbconfig = configDao.queryById(config.getId());
        if (dbconfig == null) {
            resultModel.setBresult(false);
            resultModel.setMessage("禁用配置无法修改");
            return resultModel;
        }
        dbconfig.setCkey(config.getCkey());
        dbconfig.setCvalue(config.getCvalue());
        dbconfig.setName(config.getName());
        configDao.update(dbconfig);
        resultModel.setBresult(true);
        resultModel.setMessage("修改成功");

        return resultModel;
    }

    @Override
    public ResultModel queryConfig(Config config) {
        Config dbconfig = configDao.queryById(config.getId());
        ResultModel resultModel = new ResultModel();
        resultModel.setBresult(true);
        resultModel.setObject(dbconfig);
        return resultModel;
    }

    @Override
    public ResultModel queryConfigByKey(String key) {
        Config config=configDao.queryByKey(key);
        VersionVo versionVo=JsonStringUtil.readValue(config.getCvalue(),VersionVo.class);
        ResultModel resultModel=new ResultModel();
        resultModel.setBresult(true);
        if(versionVo==null){
            resultModel.setObject(config);
        }else{
            resultModel.setObject(versionVo);
        }
        return resultModel;
    }
}
